package com.ruoyi.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.base.ch340.Ch340Configure;
import com.ruoyi.base.ch340.Ch340Service;
import com.ruoyi.base.tool.CHeck16;
import com.ruoyi.base.tool.TypeConverter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.WaterCard;
import com.ruoyi.base.service.IWaterCardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 充值Controller
 *
 * @author ruoyi
 * @date 2025-04-24
 */
@RestController
@RequestMapping("/base/card")
public class WaterCardController extends BaseController
{
    @Autowired
    private IWaterCardService waterCardService;

    @Autowired
    private Ch340Service ch340Service;


    //获取水卡信息，用于前端回显
    @GetMapping("/getInfo")
    public AjaxResult getInfo(int type){

        WaterCard waterCard = new WaterCard();

//        waterCard.setCardNum("12345678");
//        waterCard.setCardName("张三");
//        if (type==1){
//            waterCard.setCardType("1");
//        }else {
//            waterCard.setCardPay("100");
//        }

        try {
            String read = ch340Service.sendAndRead(Ch340Configure.X19);
            if (CHeck16.read(read)) {

                List<String> list = TypeConverter.value1(read);
                waterCard.setCardNum(list.get(0));

                if (type==1){
                    waterCard.setCardType("1");
                }else {
                    waterCard.setCardPay(list.get(1));
                }
            }
        } catch (Exception e) {

            return error(e.getMessage());

        }

        return success(waterCard);

    }


    /**
     * 查询充值列表
     */
    @PreAuthorize("@ss.hasPermi('card:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(WaterCard waterCard)
    {
        startPage();
        List<WaterCard> list = waterCardService.selectWaterCardList(waterCard);
        return getDataTable(list);
    }

    /**
     * 导出充值列表
     */
    @PreAuthorize("@ss.hasPermi('card:card:export')")
    @Log(title = "充值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WaterCard waterCard)
    {
        List<WaterCard> list = waterCardService.selectWaterCardList(waterCard);
        ExcelUtil<WaterCard> util = new ExcelUtil<WaterCard>(WaterCard.class);
        util.exportExcel(response, list, "充值数据");
    }

    /**
     * 获取充值详细信息
     */
    @PreAuthorize("@ss.hasPermi('card:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(waterCardService.selectWaterCardById(id));
    }

    /**
     * 新增充值
     */
    @PreAuthorize("@ss.hasPermi('card:card:add')")
    @Log(title = "充值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WaterCard waterCard)
    {

        int i;
        try {
            i = waterCardService.insertWaterCard(waterCard);
        } catch (Exception e) {
            return error(e.getMessage());
        }

        return toAjax(i);
    }

    /**
     * 修改充值
     */
    @PreAuthorize("@ss.hasPermi('card:card:edit')")
    @Log(title = "充值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WaterCard waterCard)
    {
        return toAjax(waterCardService.updateWaterCard(waterCard));
    }

    /**
     * 删除充值
     */
    @PreAuthorize("@ss.hasPermi('card:card:remove')")
    @Log(title = "充值", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(waterCardService.deleteWaterCardByIds(ids));
    }
}
